/*
 * @# FigureRedis.java 2011-12-26 下午02:40:04
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.user.redis;

import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.ianswer.px.common.redis.RedisSupport;
import com.ianswer.px.user.entity.Figure;

/**
 * 用户相关的数目缓存.
 * 
 * @author alvin hwang
 */
public class FigureRedis extends RedisSupport {

	private static final String FIGURE_PREFIX = "figure:";

	private static final String FOLLOWS = "follows";
	private static final String FANS = "fans";
	private static final String VISITS = "visits";
	private static final String POSTS = "posts";
	private static final String ALBUM = "album";
	private static final String NFANS = "nfans";
	private static final String NCOMMENT = "ncomment";
	private static final String NATME = "natme";
	private static final String UNREAD = "unread";

	public static boolean incrFollows(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), FOLLOWS, 1));
	}

	public static boolean decrFollows(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), FOLLOWS, -1));
	}

	public static boolean incrFans(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), FANS, 1));
	}

	public static boolean decrFans(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), FOLLOWS, -1));
	}

	public static boolean incrVISITS(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), VISITS, 1));
	}

	public static boolean incrPOSTS(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), POSTS, 1));
	}

	public static boolean decrPOSTS(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), POSTS, -1));
	}

	public static boolean incrALBUM(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), ALBUM, 1));
	}

	public static boolean decrALBUM(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), ALBUM, -1));
	}

	public static boolean incrNFANS(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), NFANS, 1));
	}

	public static boolean decrNFANS(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), NFANS, -1));
	}

	public static boolean incrNCOMMENT(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), NCOMMENT, 1));
	}

	public static boolean decrNCOMMENT(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), NCOMMENT, -1));
	}

	public static boolean incrNATME(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), NATME, 1));
	}

	public static boolean decrNATME(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), NATME, -1));
	}

	public static boolean incrUNREAD(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), UNREAD, 1));
	}

	public static boolean decrUNREAD(Long userId) {
		return settedIfExists(redis().hincrBy(key(userId), UNREAD, -1));
	}

	public static Figure get(Long userId) {
		Map<String, String> figureCache = redis().hgetAll(key(userId));
		if (MapUtils.isEmpty(figureCache)) {
			return null;
		}
		Figure figure = new Figure();
		figure.setFans(figureCache.get(FANS));
		figure.setFollows(figureCache.get(FOLLOWS));
		figure.setAlbum(figureCache.get(ALBUM));
		figure.setPosts(figureCache.get(POSTS));
		figure.setNatme(figureCache.get(NFANS));
		figure.setNcomment(figureCache.get(NCOMMENT));
		figure.setNfans(figureCache.get(NFANS));
		figure.setVisits(figureCache.get(VISITS));
		figure.setUnread(figureCache.get(UNREAD));
		return figure;
	}

	/**
	 * 未读信息条数
	 * @param userId
	 * @return
	 */
	public static long getUnReadNum(long userId) {
		String str_unRead = redis().hget(key(userId), UNREAD);
		if (StringUtils.isBlank(str_unRead)) {
			return 0;
		}
		return Long.parseLong(str_unRead);
	}

	/**
	 * 获取未读数并清零（在异步读取有多少条未读消息时使用)
	 * @param userId
	 * @return
	 */
	public static long getUnReadNumAndSetZero(long userId) {
		long num = getUnReadNum(userId);
		if (num > 0) {
			setZeroUnReadNum(userId);
		}
		return num;
	}

	/**
	 * 未读数清零（供首页打开时使用）
	 * @param userId
	 */
	public static void setZeroUnReadNum(long userId) {
		redis().hset(key(userId), UNREAD, "0");
	}

	private static String key(Long userId) {
		return FIGURE_PREFIX + userId;
	}
}
